// Ionic Starter App

// angular.module is a global place for creating, registering and retrieving Angular modules
// 'starter' is the name of this angular module example (also set in a <body> attribute in index.html)
// the 2nd parameter is an array of 'requires'
// 'starter.services' is found in services.js
// 'starter.controllers' is found in controllers.js
angular.module('starter', ['ionic', 'starter.services'])
//防止与jequery冲突
.config(function($compileProvider){
  $compileProvider.imgSrcSanitizationWhitelist(/^\s*(https?|ftp|mailto|file|tel):/);
})

.run(function($ionicPlatform) {
  $ionicPlatform.ready(function() {
    if(window.StatusBar) {
      // org.apache.cordova.statusbar required
      StatusBar.styleDefault();
    }
  });
})

.controller('MainCtrl', function($scope, Camera) {
//            document.getElementById('test').value='PHOTOLIBRARY';
  $scope.getPhoto = function() {
            //then=call back
        
//            alert("dad");
            
           
          
            document.getElementById('pictureSource').value = 'CAMERA';
            
            Camera.getPicture().then(function(imageURI) {
                                     console.log(imageURI);
                                     $scope.lastPhoto = imageURI;
                                     }, function(err) {
                                     console.err(err);
                                     }, {
                                     quality: 75,
                                     targetWidth: 320,
                                     targetHeight: 320,
                                     saveToPhotoAlbum: false
                                     });
  };
            
            $scope.getPhoto2 = function() {
            //then=call back
//            alert("dad2");
            
            
            
            
            
            document.getElementById('pictureSource').value = 'PHOTOLIBRARY';
            Camera.getPicture().then(function(imageURI) {
                                     console.log(imageURI);
                                     $scope.lastPhoto = imageURI;
                                     }, function(err) {
                                     console.err(err);
                                     }, {
                                     quality: 75,
                                     targetWidth: 320,
                                     targetHeight: 320,
                                     saveToPhotoAlbum: false
                                     });
            };
            

})



//加入定位功能
var app = {
    // Application Constructor
initialize: function() {
    this.bindEvents();
},
    // Bind Event Listeners
    //
    // Bind any events that are required on startup. Common events are:
    // 'load', 'deviceready', 'offline', and 'online'.
bindEvents: function() {
    document.addEventListener('deviceready', this.onDeviceReady, false);
},
    // deviceready Event Handler
    //
    // The scope of 'this' is the event. In order to call the 'receivedEvent'
    // function, we must explicitly call 'app.receivedEvent(...);'
onDeviceReady: function() {
    app.receivedEvent('deviceready');
},
    
    // Update DOM on a Received Event
receivedEvent: function(id) {
    var parentElement = document.getElementById(id);
    var listeningElement = parentElement.querySelector('.listening');
    var receivedElement = parentElement.querySelector('.received');
    
    listeningElement.setAttribute('style', 'display:none;');
    receivedElement.setAttribute('style', 'display:block;');
    
    console.log('Received Event: ' + id);
    
    navigator.geolocation.getCurrentPosition(this.onLocationFound, this.onLocationError, { maximumAge: 30000, timeout: 6000});
},
    
        onLocationFound: function (position) {
            console.log('Position Found:', position);
            alert('position: ' + [position.coords.latitude, position.coords.latitude].join(', '));
        },
    
        onLocationError: function (err) {
            console.log('Position Error:', err);
            alert('error ' + [err.code, err.message].join(': '));
        }
    
    
    
    
    
};

app.initialize();